<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur ()){
    include_once (DS . "backoffice/include/header.php");

    if (isset($_POST['submit']))
    {
        $contact = new ContactEntreprise();
        // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
        $form = true;
        $errMSG = array();
        if (isset($_POST['lastname']) && !empty($_POST['lastname']))
        {
            $tmp = $contact->setNomUtilisateur($_POST['lastname']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['firstname']) && !empty($_POST['firstname']))
        {
            $tmp = $contact->setPrenomUtilisateur($_POST['firstname']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['prenom'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['password']) && !empty($_POST['password']) && isset($_POST['password2']) && !empty($_POST['password2']))
        {
            $tmp = $contact->setPassword($_POST['password']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['password'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['email']) && !empty($_POST['email']))
        {
            $tmp = $contact->setEmailUtilisateur($_POST['email']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['adresse']) && !empty($_POST['adresse']))
        {
            $tmp = $contact->setAdresse1($_POST['adresse']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['add1'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['adresse2']) && !empty($_POST['adresse2']))
        {
            $tmp = $contact->setAdresse2($_POST['adresse2']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['add2'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['city']) && !empty($_POST['city']))
        {
            $tmp = $contact->setVille($_POST['city']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['ville'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if (isset($_POST['cp']) && !empty($_POST['cp']))
        {
            $tmp = $contact->setCP($_POST['cp']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['cp'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['entreprise']) && !empty($_POST['entreprise']))
        {
            $tmp = $contact->setIdEntreprise($_POST['entreprise']);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['entreprise'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if (isset($_POST['types']) && !empty($_POST['types']))
        {
            $types = implode(',', $_POST['types']);
            $tmp = $contact->setIdTypeUser($types);
            if ($tmp['boolOk'] === false)
            {
                $errMSG['types'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if ($form === true)
        {
            $contact->insert();
            redirectTo(URLBACK."_usercontact_list");
        }
    }
    $lstEntreprise = Entreprise::getList();
    $lstTypes = TypeUtilisateur::getList();

    include_once( DS . "backoffice/include/toppanel_utilisateurs.php");
    ?>

    <div id="wrapper">
    <div id="content">
        <div id="box">
            <h3 id="adduser">Ajout d'un contact entreprise</h3>
            <form id="form" method="post">
                <fieldset id="personal">
                    <legend>Informations Personnelles</legend>
                    <label for="lastname">Nom : </label>
                    <input name="lastname" id="lastname" type="text" tabindex="1" />
                    <br />
                    <label for="firstname">Prenom : </label>
                    <input name="firstname" id="firstname" type="text" tabindex="2" />
                    <br />
                    <label for="password">Password : </label>
                    <input name="password" id="password" type="text"tabindex="2" />
                    <br />
                    <label for="password2">re-Password : </label>
                    <input name="password2" id="password2" type="text"tabindex="2" />
                    <br />
                    <label for="email">Email : </label>
                    <input name="email" id="email" type="text"tabindex="2" />
                </fieldset>
                <fieldset id="address">
                    <legend>Adresse</legend>
                    <label for="adresse">Adresse : </label>
                    <input name="adresse" id="adresse" type="text" tabindex="1" />
                    <br />
                    <label for="adresse2">Adresse + : </label>
                    <input name="adresse2" id="adresse2" type="text" tabindex="1" />
                    <br />
                    <label for="cp">Code Postal : </label>
                    <input name="cp" id="cp" type="text" tabindex="1" />
                    <br />
                    <label for="city">Ville : </label>
                    <input name="city" id="city" type="text" tabindex="2" />
                    <br />
                </fieldset>
                <fieldset id="opt">
                    <legend>OPTIONS</legend>
                    <label for="entreprise">Entreprise : </label>
                    <select name="entreprise">
    <?php
    if (!empty($lstEntreprise))
    {
    foreach ($lstEntreprise as $p)
    {
    ?>
                        <option value="<?php echo $p->IdEntreprise; ?>"><?php echo $p->NomEntreprise; ?></option>
                        <?php
                    }
                }
                        ?>
                    </select>
                    <br/>
                    <label for="types">Type d'utilisateur : </label>
    <?php
                if (!empty($lstTypes))
                {
                    foreach ($lstTypes as $type)
                    {
    ?>
                    <?php echo $type->NomTypeUtilisateur; ?>
                        <input type="checkbox" name="types[]" value="<?php echo $type->IdTypeUtilisateur; ?>">
                        &nbsp;|&nbsp;
    <?php
                    }
                }
    ?>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Ajouter" />
                    <input id="button2" type="reset" />
                </div>
            </form>
            <br /><span class="error"><?php if (isset($errMSG['types']))
                    {
                        echo $errMSG['types'];
                    } ?></span>
        </div>
    </div>
    <?php
    include_once (DS . "backoffice/include/sidebar.php");
    include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>